/**
 * 
 */
package rai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import rai.blo.RegFormBLO;
import rai.dto.LoginUserDTO;
import rai.form.EditProfileForm;
import rai.util.Constants;

/**
 * @author Vu
 *
 */
public class EditProfileDAO {
    /** Log property. */
    private static Logger log = Logger
            .getLogger("EditProfileDAO.start");
    
    public String updateProfile (int empID, EditProfileForm editform) {
        log.info("EditTakenCourse.UpdateCourseInfo");
        Connection conn = null;
        String query = Constants.BLANK_VALUE;
        // ResultSet rs = null;
        PreparedStatement pStat = null;
        String error = null;
                
        LoginDAO loginDAO = new LoginDAO();
        LoginUserDTO chkUser = loginDAO.getUser(editform.getTxtOldUserName(), 
                                            editform.getTxtOldPass());
        RegFormBLO regBLO = new RegFormBLO();
        error = regBLO.checkPass(editform.getTxtNewPass());
        error = regBLO.checkUsername(editform.getTxtUserName());
   
        if ((chkUser != null) && (error == null)) {
            try {
                query = "update user u set u.UserName='" + editform.getTxtUserName()
                        + "', u.Password='" + editform.getTxtNewPass()
                        + "' where u.EmpID=" + empID;
                conn = rai.util.Connection.getConnection();
                pStat = conn.prepareStatement(query);
                pStat.executeUpdate(query);
                error = "Update Profile successfully!!!";
    
            } catch (final Exception sqlEx) {
                log.error("EditProfileDAO.updateProfile " + sqlEx);
            } finally {
                try {
                    // rs.close();
                    pStat.close();
                    conn.close();
                    
                } catch (final SQLException sqlEx) {
                    log.error("EditProfileDAO.updateProfile " + sqlEx);
                }
            }
        } else if (error == null) {
            error = "Password incorrect!!!";
        }
        log.debug("EditTakenCourseDAO.updateCourse.end");
        
        return error;
    }
}
